package com.aw.baselib.util;

/**
 * Created by hzw
 */
public class AwFormatCheckUtils {

    private AwFormatCheckUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }


    /**
     * 检测是否只有字母或者数字
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        boolean isRight = false;//定义一个boolean值，用来表示是否包含数字
        String regex = "^[a-zA-Z0-9]+$";
        isRight = str.matches(regex);
        return isRight;
    }

    /**
     * 必须包含字母和数字
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigitAll(String str) {
        boolean isRight = false;//定义一个boolean值，用来表示是否包含数字
        String regex = ".*[a-zA-Z].*[0-9]|.*[0-9].*[a-zA-Z]";
        isRight = str.matches(regex);
        return isRight;
    }


    /**
     * 判断是否符合邮箱格式
     */
    public static boolean checkEmailValid(String strEmail) {
        if (null == strEmail) {
            return false;
        }
        return strEmail.matches("[a-zA-Z0-9_]+@[a-z0-9]+(.[a-z]+){2}");
    }


    /**
     * 判断是否符合座机号格式
     *
     * @param phoneNumber
     * @return
     */
    public static boolean checkPhoneNumberValid(String phoneNumber) {
        if (null == phoneNumber) {
            return false;
        }

        /**
         * 匹配北京上海等3-8格式：(^0[1,2]{1}\\d{1}-?\\d{8}
         * 匹配其他省份等4-7/8格式:(^0[3-9]{1}\\d{2}-?\\d{7,8})
         * 匹配内部电话转接号：(-(\\d{1,4}))?$)
         */
        // 区号与座机号之间可不添加“-” 外部号码与内部号码之间必须添加“-”
        String check = "((^0[1,2]{1}\\d{1}-?\\d{8}|(^0[3-9]{1}\\d{2}-?\\d{7,8}))(-(\\d{1,4}))?$)";
        return phoneNumber.matches(check);
    }

    /**
     * 验证手机号方法
     *
     * @param strPhoneNum
     * @return
     */
    public static boolean checkMobileNumberValid(String strPhoneNum) {
        if (null == strPhoneNum) {
            return false;
        }
        /**
         * 匹配13、15、18开头手机号 排除154 开头手机号
         * 匹配170、176、177、178开头手机号
         * 匹配规则参考当前（2015-04-29）百度百科“手机号”罗列号码
         */
        String checkphone = "^(((13|18)[0-9])|(15[^4,\\D])|170|176|177|178|199|198)\\d{8}$";
        return strPhoneNum.matches(checkphone);

    }

    /**
     * 检测String是否全是中文
     *
     * @param name
     * @return
     */
    public static boolean checkNameChese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++) {
            if (!isChinese(cTemp[i])) {
                res = false;
                break;
            }
        }
        return res;
    }


    /**
     * 判定输入汉字
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

}
